<?php
class userModel extends Model{
    protected function init(){
        
    }   
    public function read($id=false){       
        $data = array();
        $q = "";
        if(!empty($id)){
            $q = "WHERE `id`={$id}";
        }
        $start = isset($_GET['start']) ? $_GET['start'] : 0;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 10;
        $query = "SELECT * FROM `users` {$q} ORDER BY `username` DESC LIMIT {$start},{$limit}";

        $result = $this->db->query($query);
        
        $nbrows = $this->db->fetchRow($this->db->query("SELECT COUNT(*) FROM `users`"));
        $nbrows = $nbrows[0];
        while($row=$this->db->fetchAssoc($result)){
            $data[]=$row;
        }
// print_r($data);
        if(MyHelpers::isAjax()){
            // prepare the metadata
            $ret['metaData'] =   $this->makeMetaData($result);
            $ret['total']    =   $nbrows;
            $ret['data']     =   $this->Char2Utf8($data);
            $ret['success']  =   true;
            $ret['message']  =   true;
            return json_encode($ret);
        }
        return $data;       
    }
    
    
 protected function Char2Utf8($var){
        if (is_array($var)) {
            $out = array();
            foreach ($var as $key => $v) {
                $out[$key] = $this->Char2Utf8($v);
            }
        } else {
            if(gettype($var)=='string'){
                $out = str_replace(chr(194), "", $var);
                $out = utf8_encode($out);
            }
            else
              $out = $var;  
        }       
        return $out;                   
    }
    
    protected function makeMetaData($result, $overrides = false){
        $metaData['idProperty'] = 'id';
        $metaData['totalProperty'] = 'total';
        $metaData['successProperty'] = 'success';
        $metaData['root'] = 'data';        
        $metaData['fields'] = $this->parseMetaDataFields($result, $overrides);        
        
        return $metaData;
    }
        //function are userd for add new user
    public function addUser() {
        if (isset($_POST["submit"])) {
            $Uname = $_POST["username"];
            $emial = $_POST["email"];
            $Md5Pass = $_POST["password"];
            $Md5Pass2 = md5($Md5Pass);
            $Pass = md5($Md5Pass2);
            $object = new UserController();
            $object->insertdb($username, $emial, $password);
        }
    }

    //function are userd for Login
    public function login() {
        if (isset($_POST["login"])) {
            $Uname = $_POST["username"];
            $Md5Pass = $_POST["password"];
            $Md5Pass2 = md5($Md5Pass);
            $Pass = md5($Md5Pass2);
            $object = new UserController();
            $result = $object->login_chk($Uname, $Pass);
            if ($result == "true") {
                echo $_SESSION['user_login_id'];

                header("location:UserProfile.php");
            } else {
                echo "Wrong Username or Password";
            }
        }
    }

    //function are userd for add AddUserProfiles
    public function AddUserProfiles() {
        if (isset($_POST["usersubmit"])) {
            $Fname = $_POST["first_name"];
            $Lname = $_POST["last_name"];
            $Mname = $_POST["middel_name"];
            $Phone = $_POST["phone"];
            $Country = $_POST["country"];
            $State = $_POST["state"];
            $City = $_POST["city"];
            $Discrp = $_POST["discriptions"];
            $Role = serialize($_POST['role']);
            if (isset($_POST["active"])) {
                $Active = $_POST['active'];
            } else {
                $Active = $_POST['active'] = 0;
            }
            if (isset($_POST["confirm"])) {
                $Confirm = $_POST['confirm'];
            } else {
                $Confirm = $_POST['confirm'] = 0;
            }

            $object = new UserController();
            $object->insertUserPrfdb($Fname, $Lname, $Mname, $Phone, $Country, $State, $City, $Discrp, $Role, $Active, $Confirm);
        }
    }

    public function CheckUserProfile($id) {
        $object = new UserController();
        $result = $object->CheckUserProfiles($id);
        if ($result) {
            return $result;
        }
    }
   
}